package com.yueban.net;


import com.yueban.model.ActivityData;
import com.yueban.model.BaseResponse;
import com.yueban.model.User;
import com.yueban.net.service.ActivityService;
import com.yueban.net.service.UserService;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by guan on 17-8-7.
 */

public class NetWorkController {
    private UserService mUserService;
    private ActivityService mActivityService;

    public NetWorkController() {
        RetrofitServiceManager manager = RetrofitServiceManager.getInstance();

        mActivityService = manager.create(ActivityService.class);
        mUserService = manager.create(UserService.class);
    }

    //执行登录请求
    public Observable<BaseResponse<User>> login(String account, String password) {
        return observable(mUserService.login(account, password));
    }

    //执行注册请求
    public Observable<BaseResponse<User>> register(String name, String account, String password) {
        return observable(mUserService.register(name, account, password));
    }

    //上传activity
    public Observable<BaseResponse> submitActivity(String content, String date, String category,
                                                   String address, List<byte[]> pictures, long userId) {
        if (pictures == null)
            return observable(mActivityService.submitActivity(content, date, category, address, userId));
        else {
            MultipartBody.Builder builder = new MultipartBody.Builder();
            builder.addFormDataPart("content", content)
                    .addFormDataPart("date", date)
                    .addFormDataPart("category", category)
                    .addFormDataPart("address", address)
                    .addFormDataPart("userId", userId + "");

            for (int i = 0; i < pictures.size(); i++) {
                builder.addFormDataPart("pictures", i + ".jpeg",
                        RequestBody.create(MediaType.parse("image/*"), pictures.get(i)));
            }
            return observable(mActivityService.submitActivity(builder.build()));
        }
    }

    //请求获取activity
    public Observable<BaseResponse<List<ActivityData>>> getActivities(String category, long lastActivityId) {
        return observable(mActivityService.getActivities(category, lastActivityId));
    }

    //通过id获取用户信息
    public Observable<BaseResponse<User>> getUserInfo(long userId) {
        return observable(mUserService.getUserInfo(userId));
    }

    //上传评论
    public Observable<BaseResponse> submitComment(long activityId, long userId, String commentText) {
        return observable(mActivityService.addComment(activityId, userId, commentText));
    }

    //查询0,参与1,退出2 当前活动,根据action判断
    public Observable<BaseResponse<Boolean>> joinActivity(long activityId, long userId, int action) {
        return observable(mActivityService.joinActivity(activityId, userId, action));
    }

    //获取参与人列表
    public Observable<BaseResponse<List<User>>> getJoinList(long activityId) {
        return observable(mActivityService.getJoinList(activityId));
    }

    //提交反馈
    public Observable<BaseResponse> submitFeedback(String name, String phone, String content) {
        return observable(mUserService.submitFeedback(name, phone, content));
    }
    //添加头像
    public Observable<BaseResponse> addUserIcon(byte[] icon,long userId) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.addFormDataPart("userIcon",userId + ".jpeg",
                RequestBody.create(MediaType.parse("image/*"), icon));
        return observable(mUserService.addUserIcon(builder.build()));
    }


    private <T> Observable<T> observable(Observable<T> observable) {
        return observable
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
}
